package com.hb.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hb.back.dto.SearchTeacherDto;
import com.hb.common.domain.EduTeacher;
import com.hb.common.service.EduTeacherService;
import com.hb.common.vo.ResultVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/teacher")
@Tag(name="讲师接口")
public class TeacherController {

    @Autowired
    EduTeacherService teacherService;

    @PostMapping("/page")
    @Operation(summary = "讲师的分页待条件查询")
    public ResultVo page(@RequestBody SearchTeacherDto searchTeacherDto,
                         @RequestParam(defaultValue = "1") Integer pagenum,
                         @RequestParam(defaultValue = "3") Integer pagesize){
        if (searchTeacherDto.getPagenum()!=null&&searchTeacherDto.getPagenum()>1){
            pagenum=searchTeacherDto.getPagenum();
        }
        LambdaQueryWrapper<EduTeacher> queryWrapper=new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(searchTeacherDto)){
           if (!ObjectUtils.isEmpty(searchTeacherDto.getName())){
               queryWrapper.like(EduTeacher::getName,searchTeacherDto.getName());
           }
           if (!ObjectUtils.isEmpty(searchTeacherDto.getLevel())){
                queryWrapper.eq(EduTeacher::getLevel,searchTeacherDto.getLevel());
           }
           if (!ObjectUtils.isEmpty(searchTeacherDto.getDates())){
               queryWrapper.between(EduTeacher::getEduCreate,searchTeacherDto.getDates().get(0),searchTeacherDto.getDates().get(1));
           }
            queryWrapper.orderByDesc(EduTeacher::getId);
            Page<EduTeacher> page = teacherService.page(new Page<>(pagenum, pagesize), queryWrapper);
            return ResultVo.success("",page);
        }
            return ResultVo.error("",null);
    }
   @PostMapping("/seaveImg/{type}")
   @Operation(summary = "图片上传")
   public ResultVo seaveImg(@PathVariable String type,MultipartFile file) throws IOException {
        String path="E:\\uploadWorkiMAGE\\"+type;
        String fileName=file.getOriginalFilename();
        String oldName=fileName.substring(fileName.lastIndexOf("."));
        String newFile=UUID.randomUUID().toString().replace("-","")+oldName;
        File file1=new File(path);
        if (!file1.exists()){
            file1.mkdirs();
        }
       System.out.println(type);
       String getName="http://localhost/"+type+"/"+newFile;
       File  upFile=new File(path+"\\"+newFile);
       file.transferTo(upFile);
       return ResultVo.success("文件上传成功!",getName);
   }

  @PutMapping("/seave")
  @PreAuthorize("hasAuthority('/teacher/save')")
  @Operation(summary = "添加讲师")
public ResultVo seaveTeacher(@RequestBody EduTeacher eduTeacher){
        try{
            eduTeacher.setSort(1);
            teacherService.save(eduTeacher);
        }catch (Exception e) {
            return ResultVo.error("用户名已存在!",null);
        }
      return ResultVo.success("添加成功!",null);
  }

    @PostMapping("/upload")
    @PreAuthorize("hasAuthority('/teacher/save')")
    @Operation(summary = "修改讲师")
    public ResultVo upload(@RequestBody EduTeacher eduTeacher){
        try{
            eduTeacher.setSort(1);
            teacherService.updateById(eduTeacher);
        }catch (Exception e) {
            return ResultVo.error("修改失败!",null);
        }
        return ResultVo.success("修改成功!",null);
    }

    @GetMapping("/del/{id}")
    @PreAuthorize("hasAuthority('/teacher/save')")
    @Operation(summary = "删除讲师")
    public ResultVo upload(@PathVariable long id){
        try{
            teacherService.removeById(id);
        }catch (Exception e) {
        return ResultVo.error("删除失败!",null);
        }
        return ResultVo.success("删除成功!",null);
    }

}
